<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="机器学习实战第一课——KNN近邻算法, 休蛰">
    <meta name="description" content="
纸上得来终觉浅，绝知此事要躬行
　　距离我“入门”机器已经半年了，之所以是假入门，是因为平时课程实在是太繁重了，刷均分，赶作业，完全顾不上认真学习。因为渐渐的觉得每周跟的组会上面研究生学长分享的那些东西听不太懂，是为基础过于薄弱，同时又进">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <title>机器学习实战第一课——KNN近邻算法 | 休蛰</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">
    
    <script src="/libs/jquery/jquery.min.js"></script>
    
<meta name="generator" content="Hexo 4.2.0"></head>


<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper head-container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">休蛰</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>标签</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/contact" class="waves-effect waves-light">
      
      <i class="fas fa-comments" style="zoom: 0.6;"></i>
      
      <span>留言板</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>友情链接</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>

<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">休蛰</div>
        <div class="logo-desc">
            
            Never really desperate, only the lost of the soul.
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			标签
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/contact" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-comments"></i>
			
			Contact
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			友情链接
		</a>
          
        </li>
        
        
    </ul>
</div>

        </div>

        
    </nav>

</header>

    



<div class="bg-cover pd-header post-cover" style="background-image: url('/medias/featureimages/11.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">机器学习实战第一课——KNN近邻算法</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        margin: 35px 0 15px 0;
        padding-left: 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        height: calc(100vh - 250px);
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #toc-content .is-active-link::before {
        background-color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/">
                                <span class="chip bg-color">机器学习</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2020-01-16
                </div>
                

                

                

                
				
                
            </div>
            
        </div>
        <hr class="clearfix">
        <div class="card-content article-card-content">
            <div id="articleContent">
                <blockquote>
<h2 id="纸上得来终觉浅，绝知此事要躬行"><a href="#纸上得来终觉浅，绝知此事要躬行" class="headerlink" title="纸上得来终觉浅，绝知此事要躬行"></a><strong>纸上得来终觉浅，绝知此事要躬行</strong></h2></blockquote>
<p>　　距离我“入门”机器已经半年了，之所以是假入门，是因为平时课程实在是太繁重了，刷均分，赶作业，完全顾不上认真学习。因为渐渐的觉得每周跟的组会上面研究生学长分享的那些东西听不太懂，是为基础过于薄弱，同时又进入了一个数据挖掘的课题组，不能当个半吊子(知识点懂一半，代码不会写)啥都不懂，所以打算趁着寒假时间还算充足，把《机器学习实战》上的算法都实现一遍（主要代码与内容学习还是参考apachecn的github所得）。<br>　　今天复现的是K-近邻算法，是所有机器学习算法中最简单的之一[wiki]，主要思想是求出测试数据与全体训练数据集之间的欧氏距离，然后对欧氏距离进行排序，返回前k个距离最短的样本数据集，这k个数据项中出现次数最多的标签即为测试数据的预测标签。为防止各种标签在k个最短距离数据项中只出现一次，k应该大于标签类别数（我瞎猜的，应该是这样）<br>　　我的python基本上处于一个入门阶段，今天实操一遍，收获颇丰，也深感能力之不足，同志仍需努力。<br>　　KNN没有训练过程，只有一次一次的计算与比较，所以说它的空间复杂度与计算复杂度都很高，下面是用python复现的代码<br>总的代码如下</p>
<h3 id="1-准备模块"><a href="#1-准备模块" class="headerlink" title="1. 准备模块"></a>1. 准备模块</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> __future__ <span class="keyword">import</span> print_function</span><br><span class="line"><span class="keyword">from</span> numpy <span class="keyword">import</span> *</span><br><span class="line"><span class="keyword">import</span> operator</span><br><span class="line"><span class="keyword">from</span> os <span class="keyword">import</span> listdir</span><br><span class="line"><span class="keyword">from</span> collections <span class="keyword">import</span> Counter</span><br><span class="line"><span class="keyword">import</span> matplotlib</span><br><span class="line"><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br></pre></td></tr></table></figure>

<ul>
<li>__future__模块可以引用未来版本python库中的函数，其实我的版本已经是3.x，所以没有必要走这一步</li>
<li>将numpy导入有多种方法，一般常用import numpy as np，这里的这种方法可以不加前缀直接调用numpy中的函数如zeros，这种方式类似于C语言中的名称空间<h3 id="2-数据读取模块"><a href="#2-数据读取模块" class="headerlink" title="2.数据读取模块"></a>2.数据读取模块</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">fr&#x3D;open(&quot;E:\MachineLearning\KNN\dataset\datingTestSet2.txt&quot;)</span><br><span class="line">numberOfLines&#x3D;len(fr.readlines())</span><br><span class="line">fr.seek(0)</span><br><span class="line">returnMat&#x3D;zeros((numberOfLines,3))</span><br><span class="line">classLabelVector&#x3D;[]</span><br><span class="line">index&#x3D;0</span><br><span class="line">for line in fr.readlines():</span><br><span class="line">    line&#x3D;line.strip()</span><br><span class="line">    listFromLine&#x3D;line.split(&#39;\t&#39;)</span><br><span class="line">    returnMat[index,:]&#x3D;listFromLine[0:3]</span><br><span class="line">    classLabelVector.append(int(listFromLine[-1]))  # classLabelVector 与 returnMat 一一对应，前者为所属标签，后者为具体数据</span><br><span class="line">    index+&#x3D;1</span><br></pre></td></tr></table></figure></li>
<li>这一部分呢，强调一下文件的读取，如今数据挖掘与数据分析方面更常用pandas，不过目前还不需要那么高深的功能，以后再用。</li>
<li>readlines()会按行读取数据（读取所有行并返回list），读取完毕之后指向最后一个字符，所以如果需要重新读取，要用seek(0)指向初始位置<br>接下来strip()函数可以删去str的头或尾的换行符或者空格或者，指定字符（需指明参数）</li>
<li>split()函数指定分隔符对str进行切片，在本代码中将str分成四个list，前三个为数据，存入returnMat中，最后一个是标签，用append()方法存入classLabelVector中</li>
<li>returnMat的数据结构为array，矩阵结构在存入数据时可以利用index索引，了解的不是很全，目前只知道这一个用法，以后再分析<h3 id="3-可视化模块"><a href="#3-可视化模块" class="headerlink" title="3.可视化模块"></a>3.可视化模块</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">labels=classLabelVector</span><br><span class="line"></span><br><span class="line">plt.figure(figsize=(<span class="number">8</span>, <span class="number">5</span>), dpi=<span class="number">80</span>)</span><br><span class="line">axes = plt.subplot(<span class="number">111</span>)</span><br><span class="line"><span class="comment"># 将三类数据分别取出来</span></span><br><span class="line"><span class="comment"># x轴代表飞行的里程数</span></span><br><span class="line"><span class="comment"># y轴代表玩视频游戏的百分比</span></span><br><span class="line">type1_x = []</span><br><span class="line">type1_y = []</span><br><span class="line">type2_x = []</span><br><span class="line">type2_y = []</span><br><span class="line">type3_x = []</span><br><span class="line">type3_y = []</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> range(len(labels)):</span><br><span class="line">    <span class="keyword">if</span> labels[i] == <span class="number">1</span>:  <span class="comment"># 不喜欢</span></span><br><span class="line">        type1_x.append(returnMat[i][<span class="number">0</span>])</span><br><span class="line">        type1_y.append(returnMat[i][<span class="number">1</span>])</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> labels[i] == <span class="number">2</span>:  <span class="comment"># 魅力一般</span></span><br><span class="line">        type2_x.append(returnMat[i][<span class="number">0</span>])</span><br><span class="line">        type2_y.append(returnMat[i][<span class="number">2</span>])</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> labels[i] == <span class="number">3</span>:  <span class="comment"># 极具魅力</span></span><br><span class="line">        type3_x.append(returnMat[i][<span class="number">0</span>])</span><br><span class="line">        type3_y.append(returnMat[i][<span class="number">3</span>])</span><br><span class="line"></span><br><span class="line">type1 = axes.scatter(type1_x, type1_y, s=<span class="number">20</span>, c=<span class="string">'red'</span>)</span><br><span class="line">type2 = axes.scatter(type2_x, type2_y, s=<span class="number">40</span>, c=<span class="string">'green'</span>)</span><br><span class="line">type3 = axes.scatter(type3_x, type3_y, s=<span class="number">50</span>, c=<span class="string">'blue'</span>)</span><br><span class="line"></span><br><span class="line">plt.xlabel(<span class="string">'Miles earned per year'</span>)</span><br><span class="line">plt.ylabel(<span class="string">'Percentage of events spent playing video games'</span>)</span><br><span class="line">axes.legend((type1, type2, type3), (<span class="string">u'dislike'</span>, <span class="string">u'Charismatic'</span>, <span class="string">u'Very attractive'</span>), loc=<span class="number">2</span>,)</span><br><span class="line"></span><br><span class="line">plt.show()</span><br></pre></td></tr></table></figure>
为了让画出来的图好看，指定不同标签不同颜色，所以画点的时候分三次，得到的图如下所示<img src="https://img2018.cnblogs.com/common/1675876/202001/1675876-20200112001536955-2127082585.png" alt="可视化"><h3 id="4-归一化"><a href="#4-归一化" class="headerlink" title="4.归一化"></a>4.归一化</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">normMat=zeros((numberOfLines,<span class="number">3</span>))</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> [<span class="number">0</span>,<span class="number">1</span>,<span class="number">2</span>]:</span><br><span class="line">    max=returnMat[:,i].max()</span><br><span class="line">    min=returnMat[:,i].min()</span><br><span class="line">    <span class="keyword">for</span> j <span class="keyword">in</span> range(<span class="number">1000</span>):</span><br><span class="line">        normMat[j,i]=(returnMat[j,i]-min)/(max-min)</span><br><span class="line"><span class="comment"># done</span></span><br></pre></td></tr></table></figure>
归一化是将所有数据都映射到一个固定的区间之中，上述方法是在[0,1]区间之间，这么做的目的是使各类数据的权重保持一致，保证某一类数据的影响不会强于其他<br><strong>常见归一化方法有:</strong></li>
<li>线性转换： newValue=(oldValue-min)/(max-min) max和min为该组数据集中的最大最小特征值</li>
<li>对数转换： y=log10(x)</li>
<li>反余切转换：y=arctan(x)*2/PI<h3 id="5-KNN算法部分"><a href="#5-KNN算法部分" class="headerlink" title="5.KNN算法部分"></a>5.KNN算法部分</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">inx=[<span class="number">0.40166314</span>, <span class="number">0.56719748</span>, <span class="number">0.52034602</span>] <span class="comment"># 作为输入的样本</span></span><br><span class="line">dataSetSize=normMat.shape[<span class="number">0</span>]</span><br><span class="line">diffMat = tile(inx, (dataSetSize,<span class="number">1</span>))-normMat</span><br><span class="line">sqDiffMat = diffMat**<span class="number">2</span></span><br><span class="line">distances=(sqDiffMat.sum(axis=<span class="number">1</span>))**<span class="number">0.5</span></span><br><span class="line">sortedDistIndicies = distances.argsort() <span class="comment"># argsort()函数是将x中的元素从小到大排列，提取其对应的index(索引号)，所以并不会影响对应的label的值</span></span><br><span class="line"><span class="comment"># k取5</span></span><br><span class="line">classCount=&#123;&#125;</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">5</span>):</span><br><span class="line">    voteIlabel = labels[sortedDistIndicies[i]]</span><br><span class="line">    classCount[voteIlabel] = classCount.get(voteIlabel,<span class="number">0</span>) + <span class="number">1</span></span><br><span class="line">sortedClassCount = sorted(classCount.items(), key=operator.itemgetter(<span class="number">1</span>), reverse=<span class="literal">True</span>)</span><br></pre></td></tr></table></figure>
其实到了这一部分发现难度反而没有前面的高，可能是因为这个算法比较简单<br><strong>值得注意的点有:</strong></li>
<li>tile()函数用来对数组进行复制第二个参数(x,y)分别表示行复制多少次，列复制多少次</li>
<li>argsort()函数是将x中的元素从小到大排列，提取其对应的index(索引号)，所以并不会影响对应的label的值</li>
<li>classCount是一个构造字典的过程，对字典还不太熟，但今天的一天的已经结束了，没精力再去深究了，不干了，睡大觉<br><strong>第一天的工作，over！</strong></li>
</ul>
<hr>
<p>DAY 2 对KNN算法进行一个总结（来自apachecn上的小结，在此做一个备份）：<br><strong>近似误差与估计误差</strong></p>
<ul>
<li>近似误差：可以理解为对现有训练集的训练误差。</li>
<li>估计误差：可以理解为对测试集的测试误差。</li>
</ul>
<p>近似误差关注训练集，如果近似误差小了会出现过拟合的现象，对现有的训练集能有很好的预测，但是对未知的测试样本将会出现较大偏差的预测。模型本身不是最接近最佳模型。</p>
<p>估计误差关注测试集，估计误差小了说明对未知数据的预测能力好。模型本身最接近最佳模型。</p>
<p>推荐使用交叉验证<a href="https://zhuanlan.zhihu.com/p/24825503" target="_blank" rel="noopener">[知乎]</a><a href="https://zh.wikipedia.org/wiki/%E4%BA%A4%E5%8F%89%E9%A9%97%E8%AD%89" target="_blank" rel="noopener">[wiki]</a><a href="https://blog.csdn.net/lhx878619717/article/details/49079785" target="_blank" rel="noopener">[CSDN]</a>选取合适的k值（交叉验证是一种评估统计分析、机器学习算法对独立于训练数据的数据集的泛化能力）</p>
<p><strong>距离度量 Metric/Distance Measure</strong></p>
<p>度量距离除了本文中用的欧氏距离之外，还可以是Minkowski距离或者曼哈顿距离（更多细节可以参看 sklearn 中 valid_metric 部分）</p>
<p><strong>算法</strong><br>算法：（sklearn 上有三种）</p>
<ul>
<li>Brute Force 暴力计算/线性扫描（数据结构中学过的BF算法，暴力模式匹配）</li>
<li>KD Tree 使用二叉树根据数据维度来平分参数空间<a href="https://www.cnblogs.com/eyeszjwang/articles/2429382.html" target="_blank" rel="noopener">[博客园]</a>。对高维数据处理时经常会用到</li>
<li>Ball Tree 使用一系列的超球体来平分训练数据集。（Ball Tree了解的不是很多）</li>
</ul>
<p><font color=red><strong>最后，在这里附上apachecn/AiLearning的<a href="https://github.com/apachecn/AiLearning" target="_blank" rel="noopener">github</a>，里面有非常丰富的AI领域学习资料，收获颇丰，十分感谢</strong></font></p>

            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://xiuzhedorothy.github.io" rel="external nofollow noreferrer">宇航猫休蛰</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://xiuzhedorothy.github.io/2020/01/16/ji-qi-xue-xi-shi-zhan-di-yi-ke-knn-jin-lin-suan-fa/">https://xiuzhedorothy.github.io/2020/01/16/ji-qi-xue-xi-shi-zhan-di-yi-ke-knn-jin-lin-suan-fa/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="https://xiuzhedorothy.github.io" target="_blank">宇航猫休蛰</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/tags/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/">
                                    <span class="chip bg-color">机器学习</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">

<div id="article-share">
    
    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
        </div>
    </div>

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="far fa-dot-circle"></i>&nbsp;本篇
            </div>
            <div class="card">
                <a href="/2020/01/16/ji-qi-xue-xi-shi-zhan-di-yi-ke-knn-jin-lin-suan-fa/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/11.jpg" class="responsive-img" alt="机器学习实战第一课——KNN近邻算法">
                        
                        <span class="card-title">机器学习实战第一课——KNN近邻算法</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
纸上得来终觉浅，绝知此事要躬行
　　距离我“入门”机器已经半年了，之所以是假入门，是因为平时课程实在是太繁重了，刷均分，赶作业，完全顾不上认真学习。因为渐渐的觉得每周跟的组会上面研究生学长分享的那些东西听不太懂，是为基础过于薄弱，同时又进
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2020-01-16
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-user fa-fw"></i>
                            宇航猫休蛰
                            
                        </span>
                    </div>
                </div>

                
                <div class="card-action article-tags">
                    
                    <a href="/tags/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/">
                        <span class="chip bg-color">机器学习</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2020/01/16/xin-de-bo-ke-di-zhi/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/12.jpg" class="responsive-img" alt="新的博客地址">
                        
                        <span class="card-title">新的博客地址</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            哈，github运行网页实在是太慢了，现在在码云上也开了个仓库（gitee pages），网址如下点击这里

                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2020-01-16
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-user fa-fw"></i>
                            宇航猫休蛰
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/%E5%8D%9A%E5%AE%A2%E8%BF%90%E7%BB%B4/">
                        <span class="chip bg-color">博客运维</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>

    
<!-- 代码块复制 -->

<script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>


<!-- 代码块折行 -->

<style type="text/css">
code[class*="language-"], pre[class*="language-"] { white-space: pre !important; }
</style>

    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>



    <footer class="page-footer bg-color">
    <div class="container row center-align" style="margin-bottom: 0px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            <span id="year">2019</span>
            <a href="https://xiuzhedorothy.github.io" target="_blank">宇航猫休蛰</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
            
            
            
            
            
            <span id="busuanzi_container_site_pv">
                |&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;<span id="busuanzi_value_site_pv"
                    class="white-color"></span>&nbsp;次
            </span>
            
            
            <span id="busuanzi_container_site_uv">
                |&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;<span id="busuanzi_value_site_uv"
                    class="white-color"></span>&nbsp;人
            </span>
            
            <br>
            
            <br>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/xiuzheDorothy" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:820915112@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=820915112" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 820915112" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







    <a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
        <i class="fas fa-rss"></i>
    </a>

</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script src="/js/search.js"></script>
<script type="text/javascript">
$(function () {
    searchFunc("/" + "search.xml", 'searchInput', 'searchResult');
});
</script>
    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>

    <!-- Global site tag (gtag.js) - Google Analytics -->


    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    

    

    
    
    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
